home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / cpu / m6502 / opsc02.h < prev    next >
Text File  |  2000-05-08  |  11KB  |  308 lines

  1. /*****************************************************************************
  2.  *
  3.  *     m6502ops.h
  4.  *     Addressing mode and opcode macros for 6502,65c02,65sc02,6510,n2a03 CPUs
  5.  *
  6.  *     Copyright (c) 1998,1999,2000 Juergen Buchmueller, all rights reserved.
  7.  *     65sc02 core Copyright (c) 2000 Peter Trauner, all rights reserved.
  8.  *
  9.  *     - This source code is released as freeware for non-commercial purposes.
  10.  *     - You are free to use and redistribute this code in modified or
  11.  *       unmodified form, provided you list me in the credits.
  12.  *     - If you modify this source code, you must add a notice to each modified
  13.  *       source file that it has been changed.  If you're a nice person, you
  14.  *       will clearly mark each change too.  :)
  15.  *     - If you wish to use this for commercial purposes, please contact me at
  16.  *       pullmoll@t-online.de
  17.  *     - The author of this copywritten work reserves the right to change the
  18.  *       terms of its usage and license at any time, including retroactively
  19.  *     - This entire notice must remain in the source code.
  20.  *
  21.  *****************************************************************************/
  22.  
  23. /***************************************************************
  24.  *    EA = indirect (only used by JMP)
  25.  * correct overflow handling
  26.  ***************************************************************/
  27. #undef EA_IND
  28. #define EA_IND                                                    \
  29.     EA_ABS;                                                     \
  30.     tmp = RDMEM(EAD);                                            \
  31.     if (EAL==0xff) m6502_ICount++;                                \
  32.     EAD++;                                                        \
  33.     EAH = RDMEM(EAD);                                            \
  34.     EAL = tmp
  35.  
  36. /***************************************************************
  37.  *    EA = zero page indirect (65c02 pre indexed w/o X)
  38.  ***************************************************************/
  39. #define EA_ZPI                                                    \
  40.     ZPL = RDOPARG();                                            \
  41.     EAL = RDMEM(ZPD);                                            \
  42.     ZPL++;                                                        \
  43.     EAH = RDMEM(ZPD)
  44.  
  45. /***************************************************************
  46.  *    EA = indirect plus x (only used by 65c02 JMP)
  47.  ***************************************************************/
  48. #define EA_IAX                                                    \
  49.      EA_ABS;                                                    \
  50.      if (EAL + X > 0xff) /* assumption; probably wrong ? */     \
  51.          m6502_ICount--;                                        \
  52.      EAW += X;                                                    \
  53.      tmp = RDMEM(EAD);                                            \
  54.      if (EAL==0xff) m6502_ICount++;                             \
  55.      EAD++;                                                     \
  56.      EAH = RDMEM(EAD);                                            \
  57.      EAL = tmp
  58.  
  59. #define RD_ZPI    EA_ZPI; tmp = RDMEM(EAD)
  60.  
  61. /* write a value from tmp */
  62. #define WR_ZPI    EA_ZPI; WRMEM(EAD, tmp)
  63.  
  64. /***************************************************************
  65.  ***************************************************************
  66.  *            Macros to emulate the 65C02 opcodes
  67.  ***************************************************************
  68.  ***************************************************************/
  69.  
  70.  
  71. /* 65C02 ********************************************************
  72.  *    ADC Add with carry
  73.  * different setting of flags in decimal mode
  74.  ***************************************************************/
  75. #undef ADC
  76. #define ADC                                                     \
  77.     if (P & F_D)                                                \
  78.     {                                                            \
  79.         int c = (P & F_C);                                        \
  80.         int lo = (A & 0x0f) + (tmp & 0x0f) + c;                 \
  81.         int hi = (A & 0xf0) + (tmp & 0xf0);                     \
  82.         P &= ~(F_V | F_C);                                        \
  83.         if( lo > 0x09 )                                         \
  84.         {                                                        \
  85.             hi += 0x10;                                         \
  86.             lo += 0x06;                                         \
  87.         }                                                        \
  88.         if( ~(A^tmp) & (A^hi) & F_N )                            \
  89.             P |= F_V;                                            \
  90.         if( hi > 0x90 )                                         \
  91.             hi += 0x60;                                         \
  92.         if( hi & 0xff00 )                                        \
  93.             P |= F_C;                                            \
  94.         A = (lo & 0x0f) + (hi & 0xf0);                            \
  95.     }                                                            \
  96.     else                                                        \
  97.     {                                                            \
  98.         int c = (P & F_C);                                        \
  99.         int sum = A + tmp + c;                                    \
  100.         P &= ~(F_V | F_C);                                        \
  101.         if( ~(A^tmp) & (A^sum) & F_N )                            \
  102.             P |= F_V;                                            \
  103.         if( sum & 0xff00 )                                        \
  104.             P |= F_C;                                            \
  105.         A = (UINT8) sum;                                        \
  106.     }                                                            \
  107.     SET_NZ(A)
  108.  
  109. /* 65C02 ********************************************************
  110.  *    SBC Subtract with carry
  111.  * different setting of flags in decimal mode
  112.  ***************************************************************/
  113. #undef SBC
  114. #define SBC                                                     \
  115.     if (P & F_D)                                                \
  116.     {                                                            \
  117.         int c = (P & F_C) ^ F_C;                                \
  118.         int sum = A - tmp - c;                                    \
  119.         int lo = (A & 0x0f) - (tmp & 0x0f) - c;                 \
  120.         int hi = (A & 0xf0) - (tmp & 0xf0);                     \
  121.         P &= ~(F_V | F_C);                                        \
  122.         if( (A^tmp) & (A^sum) & F_N )                            \
  123.             P |= F_V;                                            \
  124.         if( lo & 0xf0 )                                         \
  125.             lo -= 6;                                            \
  126.         if( lo & 0x80 )                                         \
  127.             hi -= 0x10;                                         \
  128.         if( hi & 0x0f00 )                                        \
  129.             hi -= 0x60;                                         \
  130.         if( (sum & 0xff00) == 0 )                                \
  131.             P |= F_C;                                            \
  132.         A = (lo & 0x0f) + (hi & 0xf0);                            \
  133.     }                                                            \
  134.     else                                                        \
  135.     {                                                            \
  136.         int c = (P & F_C) ^ F_C;                                \
  137.         int sum = A - tmp - c;                                    \
  138.         P &= ~(F_V | F_C);                                        \
  139.         if( (A^tmp) & (A^sum) & F_N )                            \
  140.             P |= F_V;                                            \
  141.         if( (sum & 0xff00) == 0 )                                \
  142.             P |= F_C;                                            \
  143.         A = (UINT8) sum;                                        \
  144.     }                                                            \
  145.     SET_NZ(A)
  146.  
  147. /* 65C02 *******************************************************
  148.  *    BBR Branch if bit is reset
  149.  ***************************************************************/
  150. #define BBR(bit)                                                \
  151.     BRA(!(tmp & (1<<bit)))
  152.  
  153. /* 65C02 *******************************************************
  154.  *    BBS Branch if bit is set
  155.  ***************************************************************/
  156. #define BBS(bit)                                                \
  157.     BRA(tmp & (1<<bit))
  158.  
  159. /* 65c02 ********************************************************
  160.  *    BRK Break
  161.  *    increment PC, push PC hi, PC lo, flags (with B bit set),
  162.  *    set I flag, reset D flag and jump via IRQ vector
  163.  ***************************************************************/
  164. #undef BRK
  165. #define BRK                                                     \
  166.     PCW++;                                                        \
  167.     PUSH(PCH);                                                    \
  168.     PUSH(PCL);                                                    \
  169.     PUSH(P | F_B);                                                \
  170.     P = (P | F_I) & ~F_D;                                        \
  171.     PCL = RDMEM(M6502_IRQ_VEC);                                 \
  172.     PCH = RDMEM(M6502_IRQ_VEC+1);                                \
  173.     CHANGE_PC
  174.  
  175.  
  176. /* 65C02 *******************************************************
  177.  *    DEA Decrement accumulator
  178.  ***************************************************************/
  179. #define DEA                                                     \
  180.     A = (UINT8)--A;                                             \
  181.     SET_NZ(A)
  182.  
  183. /* 65C02 *******************************************************
  184.  *    INA Increment accumulator
  185.  ***************************************************************/
  186. #define INA                                                     \
  187.     A = (UINT8)++A;                                             \
  188.     SET_NZ(A)
  189.  
  190. /* 65C02 *******************************************************
  191.  *    PHX Push index X
  192.  ***************************************************************/
  193. #define PHX                                                     \
  194.     PUSH(X)
  195.  
  196. /* 65C02 *******************************************************
  197.  *    PHY Push index Y
  198.  ***************************************************************/
  199. #define PHY                                                     \
  200.     PUSH(Y)
  201.  
  202. /* 65C02 *******************************************************
  203.  *    PLX Pull index X
  204.  ***************************************************************/
  205. #define PLX                                                     \
  206.     PULL(X); \
  207.     SET_NZ(X)
  208.  
  209. /* 65C02 *******************************************************
  210.  *    PLY Pull index Y
  211.  ***************************************************************/
  212. #define PLY                                                     \
  213.     PULL(Y); \
  214.     SET_NZ(Y)
  215.  
  216. /* 65C02 *******************************************************
  217.  *    RMB Reset memory bit
  218.  ***************************************************************/
  219. #define RMB(bit)                                                \
  220.     tmp &= ~(1<<bit)
  221.  
  222. /* 65C02 *******************************************************
  223.  *    SMB Set memory bit
  224.  ***************************************************************/
  225. #define SMB(bit)                                                \
  226.     tmp |= (1<<bit)
  227.  
  228. /* 65C02 *******************************************************
  229.  * STZ    Store zero
  230.  ***************************************************************/
  231. #define STZ                                                     \
  232.     tmp = 0
  233.  
  234. /* 65C02 *******************************************************
  235.  * TRB    Test and reset bits
  236.  ***************************************************************/
  237. #define TRB                                                     \
  238.     SET_Z(tmp&A);                                                \
  239.     tmp &= ~A
  240.  
  241. /* 65C02 *******************************************************
  242.  * TSB    Test and set bits
  243.  ***************************************************************/
  244. #define TSB                                                     \
  245.     SET_Z(tmp&A);                                                \
  246.     tmp |= A
  247.  
  248.  
  249. /***************************************************************
  250.  ***************************************************************
  251.  *            Macros to emulate the N2A03 opcodes
  252.  ***************************************************************
  253.  ***************************************************************/
  254.  
  255.  
  256. /* N2A03 *******************************************************
  257.  *    ADC Add with carry - no decimal mode
  258.  ***************************************************************/
  259. #define ADC_NES                                                 \
  260.     {                                                            \
  261.         int c = (P & F_C);                                        \
  262.         int sum = A + tmp + c;                                    \
  263.         P &= ~(F_V | F_C);                                        \
  264.         if( ~(A^tmp) & (A^sum) & F_N )                            \
  265.             P |= F_V;                                            \
  266.         if( sum & 0xff00 )                                        \
  267.             P |= F_C;                                            \
  268.         A = (UINT8) sum;                                        \
  269.     }                                                            \
  270.     SET_NZ(A)
  271.  
  272. /* N2A03 *******************************************************
  273.  *    SBC Subtract with carry - no decimal mode
  274.  ***************************************************************/
  275. #define SBC_NES                                                 \
  276.     {                                                            \
  277.         int c = (P & F_C) ^ F_C;                                \
  278.         int sum = A - tmp - c;                                    \
  279.         P &= ~(F_V | F_C);                                        \
  280.         if( (A^tmp) & (A^sum) & F_N )                            \
  281.             P |= F_V;                                            \
  282.         if( (sum & 0xff00) == 0 )                                \
  283.             P |= F_C;                                            \
  284.         A = (UINT8) sum;                                        \
  285.     }                                                            \
  286.     SET_NZ(A)
  287.  
  288.  
  289. /***************************************************************
  290.  ***************************************************************
  291.  *            Macros to emulate the 65sc02 opcodes
  292.  ***************************************************************
  293.  ***************************************************************/
  294.  
  295.  
  296. /* 65sc02 ********************************************************
  297.  *    BSR Branch to subroutine
  298.  ***************************************************************/
  299. #define BSR                                                     \
  300.     EAL = RDOPARG();                                            \
  301.     PUSH(PCH);                                                    \
  302.     PUSH(PCL);                                                    \
  303.     EAH = RDOPARG();                                            \
  304.     EAW = PCW + (INT16)(EAW-1);                                 \
  305.     PCD = EAD;                                                    \
  306.     CHANGE_PC
  307.  
  308.